'use strict';
/**
 * Created by FreezeSoul on 2016/1/15.
 */
define([
    "app/core/base",
    "jquery",
    "underscore",
    'JSONParse',
    'loglevel',
    'Handsontable'
], function (Chart, $, _, JSON, log, Handsontable) {

    //TODO:自定义组件属性
    var property = {
        ColumnSorting: undefined,
        ContextMenu: undefined,
        ManualRowResize: undefined,
        ManualColumnResize: undefined,
        ManualRowMove: undefined,
        ManualColumnMove: undefined,
        FixedRowsTop: undefined,
        FixedColumnsLeft: undefined,
        MapOption: function (option) {
            option.columnSorting = (this.ColumnSorting === true);
            option.contextMenu = (this.ContextMenu === true);
            option.manualRowResize = (this.ManualRowResize === true);
            option.manualColumnResize = (this.ManualColumnResize === true);
            option.manualRowMove = (this.ManualRowMove === true);
            option.manualColumnMove = (this.ManualColumnMove === true);
            if (this.FixedRowsTop) {
                option.fixedRowsTop = this.FixedRowsTop;
            }
            if (this.FixedColumnsLeft) {
                option.fixedColumnsLeft = this.FixedColumnsLeft;
            }
        }
    };

    var excel = Chart.extend({
        constructor: function (layout) {
            this._layout = layout;
            this._element = null;
            this._container = null;
            this._option = {
                Binding: [],
                Events: [],
                Extend: $.extend({}, property),
                Option: {
                    rowHeaders: true,
                    colHeaders: true,
                    stretchH: 'all',
                    sortIndicator: true
                }
            };
        },
        init: function (element) {
            try {
                this._container = element;
            } catch (e) {
                log.error(e);
            }
        },
        _readProperty: function () {
            try {
                if (!this._option.Extend) return;
                var propertyCopy = $.extend(true, {}, property);
                $.extend(true, propertyCopy, this._option.Extend);
                propertyCopy.MapOption(this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        _render: function (option) {
            $(this._container).empty();
            var table = new Handsontable(this._container, option);
            this._bindEvent(table);
            return table;
        },
        _bindEvent: function (element) {
            var _this = this;
            $(element).on("click", function (e) {
                if (_this._layout
                    && _this._layout.navigate
                    && _this._option.Events
                    && _this._option.Events.length > 0) {

                    var registerEvent = _.findWhere(_this._option.Events, {Event: "ExcelClick"});
                    if (registerEvent) {
                        var pageId = registerEvent.PageId;
                        var params = registerEvent.Params;
                        var args = [];
                        var dParam = _.findWhere(params, {Value: "Cell"});
                        if (dParam && dParam.ParamName) {
                            args.push({
                                ParamName: dParam.ParamName,
                                ParamValue: ""
                            });
                        }
                        _this._layout.navigate(pageId, args);
                    }
                    e.preventDefault();
                }
            });
        },
        example: function () {
            try {

                var exampleOption = {
                    data: Handsontable.helper.createSpreadsheetData(40, 6),
                    rowHeaders: true,
                    colHeaders: true,
                    stretchH: 'all',
                    sortIndicator: true,
                    columnSorting: true,
                    contextMenu: true
                };

                this._element = this._render(exampleOption);
            } catch (e) {
                log.error(e);
            }
        },
        _bindData: function (data) {
            var _this = this;
            var binding = this._option.Binding[0];
            var table = data[binding.DataSetCode];
            var tableData = [];
            var tableHeader = [];
            _.each(_this._option.Extend.columnBindings, function (binding) {
                tableHeader.push(binding.ColumnName);
            });
            this._option.Option.colHeaders = tableHeader;
            if (table && table.length > 0) {
                $.each(table, function (j, row) {
                    var rowData = [];
                    _.each(_this._option.Extend.columnBindings, function (binding) {
                        rowData.push(row[binding.Column]);
                    });
                    tableData.push(rowData);
                });
                this._option.Option.data = tableData;
            }
        },
        setData: function (data) {
            try {
                this._readProperty();

                if (data
                    && this._option.Binding
                    && this._option.Binding.length > 0
                    && this._option.Extend
                ) {
                    this._bindData(data);
                }
                this._element = this._render(this._option.Option);
            } catch (e) {
                log.error(e);
            }
        },
        setOption: function (option) {
            try {
                return this._option = $.extend(true, {}, option);
            } catch (e) {
                log.error(e);
            }
        },
        getOption: function () {
            try {
                this._readProperty();
            } catch (e) {
                log.error(e);
            }
            return this._option;
        },
        setTheme: function (theme) {
            try {
            } catch (e) {
                log.error(e);
            }
        },
        resize: function () {
            try {
                if (this._element) {
                    this._element.render();
                }
            } catch (e) {
                log.error(e);
            }
        },
        showLoading: function () {
            try {
                if (this._layout && this._layout.showLoading) {
                    this._layout.showLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        hideLoading: function () {
            try {
                if (this._layout && this._layout.hideLoading) {
                    this._layout.hideLoading(this);
                }
            } catch (e) {
                log.error(e);
            }
        },
        dispose: function () {
            try {

            } catch (e) {
                log.error(e);
            }
        },
        getElement: function () {
            return this._element;
        },
        getChart: function () {
            return this._element;
        }
    });

    return excel;
});